.Net Knowledge Nuggets: Dynamically Ignoring Unit Tests

During a client project, we had a set of unit tests that connected to a webservice hosted on a client server which utilized Windows Authentication and would reject requests coming from our local dev environments (since we, as consultants, had laptops that weren't on the client's AD domain).  This resulted in false-negative tests and in my experience, when you start having "known bad" tests that always fail, people start to ignore failing unit tests, which is not what you want.

So, to keep from having false-negative tests, I modified that particular suite of tests to auto-ignore the tests if the current environment is not on the right AD domain.  This would keep them from showing as failures on our local dev environments, but they would fully run on our build server (vs. marking them as permanently ignored, where they wouldn't run on the CI server).  Here's what I did:

        /// <summary>
        /// Will dynamically mark a test as Ignored if the system running the test is not
        /// on the SOMECLIENT domain. This is because the API will fail authentication
        /// otherwise and will provide false negative results 
        /// </summary>
        public static void IgnoreTestIfOffDomain()
        {
            try
            {
                var domain = System.DirectoryServices.ActiveDirectory.Domain.GetCurrentDomain();
                var domainName = domain.Name.ToUpper();

                if (!domainName.ToUpper().Contains("SOMEDOMAINNAME"))
                {
                    Assert.Ignore("NOT RUNNING API TESTS from current domain " + domain.Name);
                }
            }
            catch (ActiveDirectoryOperationException)
            {
                Assert.Ignore("NOT RUNNING API TESTS from current domain (domain missing)");
            }
        }

Note that an ActiveDirectoryOperationException will be thrown if the system is not currently registered on a domain, thus the try/catch.

I put this into a method which I called in the TestFixtureSetup or individual test methods, depending if I wanted to ignore the whole suite or the individual tests.